extends Control

@onready var slider: HSlider = $VBox/HSlider
@onready var value_label: Label = $VBox/Value
@onready var close_btn: Button = $VBox/Close if has_node("VBox/Close") else null

const MIN_FPS := 30
const MAX_FPS := 120

var frm: Node

func _ready():
	mouse_filter = Control.MOUSE_FILTER_IGNORE
	frm = get_tree().get_first_node_in_group("frame_rate_manager")
	# 初始化滑条范围与当前值
	slider.min_value = MIN_FPS
	slider.max_value = MAX_FPS
	slider.step = 1
	var cur := Engine.max_fps
	if cur <= 0:
		cur = 60
	slider.value = clamp(cur, MIN_FPS, MAX_FPS)
	_update_label(int(slider.value))
	# 信号
	slider.value_changed.connect(_on_slider_value_changed)
	if close_btn:
		close_btn.pressed.connect(_on_close_pressed)

func _on_slider_value_changed(v: float) -> void:
	var fps := int(round(v))
	_update_label(fps)
	if frm and frm.has_method("set_target_fps"):
		frm.set_target_fps(fps)

func _update_label(fps: int) -> void:
	value_label.text = str(fps, " FPS")

func _on_close_pressed() -> void:
	visible = false


